package apiserver

import (
	"vite-tsc-gin/internal/apiserver/config"
	"vite-tsc-gin/internal/apiserver/options"
	"vite-tsc-gin/pkg/cli"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

func NewCommand(baseName string) *cobra.Command {
	root := &cobra.Command{
		Use:   baseName,
		Short: "apiserver",
		RunE: func(cmd *cobra.Command, args []string) error {
			confile, er := cmd.Flags().GetString("config")
			if er != nil {
				return er
			}
			cli.LoadConfig(confile, "apiserver.yml")
			var option options.Options
			if er = viper.Unmarshal(&option); er != nil {
				return er
			}
			conf, _ := config.CreateConfigFromOptions(&option)
			return Run(conf)
		},
	}
	root.Flags().StringP("config", "c", "/etc/apiserver/apiserver.yml", "config file path")
	return root
}
